package com.atguigu;

import org.apache.spark.SparkConf;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

/**
 * @author yhm
 * @create 2022-12-26 9:07
 */
public class Test02_SQL {
    public static void main(String[] args) {
        // 1. 创建sparkConf配置对象
        SparkConf conf = new SparkConf().setAppName("sql").setMaster("local[*]");

        // 2. 创建sparkSession连接对象
        SparkSession spark = SparkSession.builder().config(conf).getOrCreate();

        // 3. 编写代码
        Dataset<Row> dataset = spark.read().json("input/user.json");

        // 创建视图
        dataset.createOrReplaceTempView("t1");

        // 编写sql
        Dataset<Row> result = spark.sql("select * from t1 where age > 19");

        result.show();

        // 编写sql的时候能够使用各种函数
        spark.sql("select split(name,' ') from t1").show();

        // 4. 关闭sparkSession
        spark.close();
    }
}
